package com.link.base.base.crowd.controller;

import com.link.base.base.crowd.model.Crowd;
import com.link.base.base.crowd.model.CrowdFilePath;
import com.link.base.base.crowd.service.CrowdService;
import com.link.base.core.basic.util.CodeCleanUtils;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 人群圈选
 *
 * @author 何启谦
 * @date 2019-02-14
 */
@Controller
@RequestMapping("/link/crowd")
public class CrowdController extends BasicController<Crowd> {

    @Resource
    private CrowdService crowdService;

    @Override
    public BasicService<Crowd> getBasicService() {
        return crowdService;
    }

    /**
     * 新建人群包，包含一级集合、二级集合和集合类型
     *
     * @author 何启谦
     * @date 2019-2-18
     */
    @RequestMapping(value = "/insertCrowd")
    @ResponseBody
    public Map<String, Object> insertCrowd(@RequestBody Crowd entity) {
        Map<String, Object> result = CodeCleanUtils.cast(new HashMap(16));
        try {
            Crowd newEntry = crowdService.insertCrowd(entity);
            result.put("newRow", newEntry);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 复制人群包，包含一级集合、二级集合和集合类型
     *
     * @author 何启谦
     * @date 2019-2-19
     */

    @RequestMapping(value = "/copyCrowd")
    @ResponseBody
    public Map<String, Object> copyCrowd(@RequestBody Crowd entity) {
        Map<String, Object> result = CodeCleanUtils.cast(new HashMap(16));
        try {
            Long id = entity.getId();
            if (null == id) {
                throw new BasicServiceException("id不能为空！");
            }
            Crowd newEntry = crowdService.copyCrowd(id);
            result.put("crowd", newEntry);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 根据ID查询单个完整的人群包，包含一级集合、二级集合和集合类型
     *
     * @param entity 人群包ID
     * @return 完整的人群包
     */

    @RequestMapping(value = "/queryFullCrowdById")
    @ResponseBody
    public Map<String, Object> queryFullCrowdById(@RequestBody Crowd entity) {
        Map<String, Object> result = CodeCleanUtils.cast(new HashMap(16));
        try {
            Long id = entity.getId();
            if (null == id) {
                throw new BasicServiceException("id不能为空！");
            }
            Crowd crowd = crowdService.queryFullCrowdById(id);
            result.put("success", true);
            result.put("crowd", crowd);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 失效人群包
     * @author 何启谦
     * @date 2019-2-19
     */
    @RequestMapping(value = "/deleteById")
    @ResponseBody
    @Override
    public Map<String, Object> deleteById(@JsonParam Crowd entity, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = CodeCleanUtils.cast(new HashMap(16));
        try {
            Long id = entity.getId();
            if (id == null) {
                throw new BasicServiceException("id不能为空！");
            }
            crowdService.invalidate(id);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 更新人群包
     *
     * @author 何启谦
     * @date 2019-2-20
     */
 /*   @RequestMapping(value ="/update")
    @ResponseBody
    public Map<String, Object> update(@RequestBody Crowd entity) {
        Map<String, Object> result = CodeCleanUtils.cast(new HashMap(16));
        try {
            Crowd crowd = crowdService.queryById(entity.getId());
            crowd.setCrowdName(entity.getCrowdName());
            crowd.setCrowdDesc(entity.getCrowdDesc());
            crowdService.update(crowd);
            result.put("newRow", crowd);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }*/

    /**
     * 修改定时更新
     *
     * @author 何启谦
     * @date 2019-2-21
     */

    @RequestMapping(value = "/cronFlagUpdate")
    @ResponseBody
    public Map<String, Object> cronFlagUpdate(@RequestBody Crowd entity) {
        Map<String, Object> result = CodeCleanUtils.cast(new HashMap(16));
        try {
            String cronFlag = entity.getCronFlag();
            //开启
            String open = "Y";
            //关闭
            String close = "N";
            if (!StringUtils.equals(cronFlag, open) && !StringUtils.equals(cronFlag, close)) {
                throw new BasicServiceException("参数错误");
            }
            Crowd crowd = crowdService.queryById(entity.getId());
            crowd.setCronFlag(cronFlag);
            crowdService.update(crowd);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * @Description //根据人群id生成人群包数据
     * @Param crowdList 人群包列表
     * @return 文件路径列表
     * @Author zhiqiang.jiang
     * @Date    2019/5/30-15:15
     */
    @RequestMapping({"/uploadCrowdToTdc"})
    @ResponseBody
    public Map<String, Object> uploadCrowdToTdc(@RequestBody List<Crowd> crowdList) {
        Map<String, Object> result = CodeCleanUtils.cast(new HashMap(16));
        try {
            List<CrowdFilePath> fileContent =  crowdService.uploadCrowdToTdc(crowdList);
            result.put("success", true);
            result.put("result", fileContent);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
